package game.client.net;

import game.common.net.GameHandler;
import game.common.world.Vector;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;

import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;

public class TestFirstDataHandler extends GameHandler implements IDataHandler{

	private ConnectionListener connectionListener;
	private ObjectPositionListener positionUpdateListener;
	
	public TestFirstDataHandler(ConnectionListener connectionListener,
								ObjectPositionListener positionUpdateListener) {
		this.connectionListener = connectionListener;
		this.positionUpdateListener = positionUpdateListener;
	}

	@Override
	public boolean onData(INonBlockingConnection nbc) throws IOException,
			BufferUnderflowException, ClosedChannelException,
			MaxReadSizeExceededException {
		
		String playerName = readPlayerName(nbc);
		Vector playerVector = readVector(nbc);
		
		connectionListener.connected(playerName, playerVector);
		
		nbc.setHandler(new InGameDataHandler(this.positionUpdateListener));
		
		return true;
	}
}
